package cn.cnf.common;

import cn.cnf.common.enums.StatusCode;
import lombok.Data;
import lombok.Setter;

/**
 * @author ZhuMeiYi
 * @date 2020/12/3 17:45
 * @description
 */
@Data
public class Result<T> {
    private Integer code;
    private String message;
    @Setter
    private T data;

    public static Result success() {
        Result result = new Result<>();
        result.setStatusCode(StatusCode.SUCCESS);
        return result;
    }

    public static Result success(Object data) {
        Result result = new Result<>();
        result.setStatusCode(StatusCode.SUCCESS);
        result.setData(data);
        return result;
    }

    public static Result warn(String message) {
        Result result = new Result<>();
        result.setStatusCode(StatusCode.WARN);
        result.setMessage(message);
        return result;
    }

    public static Result error(String message) {
        Result result = new Result<>();
        result.setStatusCode(StatusCode.SYSTEM_ERROR);
        result.setMessage(message);
        return result;
    }

    private void setStatusCode(StatusCode statusCode) {
        this.code = statusCode.getCode();
        this.message = statusCode.getMessage();
    }

}
